//
//  MeasureView.m
//  iOSProjectFramework
//
//  Created by 洪冬介 on 2019/10/31.
//  Copyright © 2019 洪冬介. All rights reserved.
//

#import "MeasureView.h"

#define CVWidth 355
#define CVHeight 106

#define ItemWidth 45
#define ItemHeight 15

#define RowCount 5
#define ColumnCount 6

#define leftMargin 15

@interface MeasureView()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, copy) NSString *desc;

@end

@implementation MeasureView

//- (NSArray *)dataSource{
//    return @[
//        @[@"尺码",@"衣长",@"胸围",@"肩宽",@"袖长",@"-"],
//        @[@"S",@"-",@"-",@"-",@"-",@"-"],
//        @[@"M",@"-",@"-",@"-",@"-",@"-"],
//        @[@"L",@"-",@"-",@"-",@"-",@"-"],
//        @[@"XL",@"-",@"-",@"-",@"-",@"-"],
//    ];
//}

//- (NSString*)desc{
//    return @"";
//}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc] init];
        CGFloat itemWidth = ItemWidth;
        CGFloat itemHeight = ItemHeight;
        flowLayout.itemSize = CGSizeMake(itemWidth, itemHeight);
        flowLayout.minimumInteritemSpacing = 1.0 * (CVWidth - ItemWidth * ColumnCount - leftMargin * 2) / (ColumnCount - 1); //项间隔
//        flowLayout.minimumLineSpacing = 20; //行间隔
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        [self addSubview:_collectionView];
        _collectionView.backgroundColor = CLEARCOLOR;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(CVWidth);
            make.height.mas_equalTo(CVHeight);
            make.left.mas_equalTo(10);
            make.top.mas_equalTo(242);
        }];
    }
    return _collectionView;
}

- (void)setDataSource:(NSArray *)dataSource{
    _dataSource = dataSource;
    [self.collectionView reloadData];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView{
    UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"模特尺码图"]];
    [self addSubview:iv];
    [iv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    [self.collectionView registerClass:[MeasureViewCell class] forCellWithReuseIdentifier:@"MeasureViewCell"];
    
    _descLab = [UILabel createLabelWithFontSize:10 andTextColor:[UIColor blackColor] andText:self.desc];
    [self addSubview:_descLab];
    _descLab.textAlignment = NSTextAlignmentCenter;
    [_descLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.collectionView.mas_left);
        make.right.mas_equalTo(self.collectionView.mas_right);
        make.bottom.mas_equalTo(self.collectionView.mas_top).offset(-12);
    }];

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return RowCount;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return ColumnCount;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    CGFloat topMargin = 14;
    CGFloat marginV = (CVHeight - RowCount * ItemHeight - topMargin * 2) / (RowCount - 1);
    
    if (section == 0) {
        return UIEdgeInsetsMake(topMargin, leftMargin, marginV, leftMargin);
    }else if(section == (RowCount - 1)){
        return UIEdgeInsetsMake(marginV, leftMargin, topMargin, leftMargin);
    }else{
        return UIEdgeInsetsMake(marginV, leftMargin, marginV, leftMargin);
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MeasureViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MeasureViewCell" forIndexPath:indexPath];
    cell.titleLab.text = self.dataSource[indexPath.section][indexPath.row];

    return cell;
}

@end

@implementation MeasureViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView{
    _titleLab = [UILabel createLabelWithFontSize:10 andTextColor:[UIColor blackColor] andText:@"-"];
    [self.contentView addSubview:_titleLab];
    _titleLab.textAlignment = NSTextAlignmentCenter;
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}


@end
